#!/bin/bash

# 语音处理系统安装脚本

echo "============================================"
echo "语音处理系统：人声提取与方言处理 - 安装脚本"
echo "============================================"

# 检查Python版本
python_version=$(python3 --version 2>&1 | awk '{print $2}')
echo "检测到Python版本: $python_version"

if [[ -z "$python_version" ]]; then
    echo "错误: 未检测到Python3，请先安装Python 3.6或更高版本。"
    exit 1
fi

# 检查pip
echo "检查pip..."
if ! command -v pip3 &> /dev/null; then
    echo "错误: 未检测到pip3，请先安装pip。"
    exit 1
fi

# 创建并激活虚拟环境
echo "创建Python虚拟环境..."
python3 -m venv venv
source venv/bin/activate

# 安装依赖包
echo "安装依赖包..."
pip install --upgrade pip
pip install -r requirements.txt

# 检查FFmpeg
echo "检查FFmpeg..."
if ! command -v ffmpeg &> /dev/null; then
    echo "警告: 未检测到FFmpeg"
    
    # 根据操作系统提供安装建议
    if [[ "$OSTYPE" == "darwin"* ]]; then
        echo "在macOS上安装FFmpeg的命令: brew install ffmpeg"
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        echo "在Ubuntu/Debian上安装FFmpeg的命令: sudo apt-get install ffmpeg"
        echo "在CentOS/RHEL上安装FFmpeg的命令: sudo yum install ffmpeg"
    else
        echo "请访问https://ffmpeg.org/download.html下载并安装FFmpeg"
    fi
else
    echo "FFmpeg已安装: $(ffmpeg -version | head -n 1)"
fi

# 测试Spleeter安装
echo "设置Spleeter..."
pip install spleeter
python -c "import spleeter" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "警告: Spleeter安装可能有问题，请检查。"
else
    echo "Spleeter已成功安装"
fi

echo ""
echo "安装完成！"
echo ""
echo "使用方法:"
echo "1. 激活虚拟环境:"
echo "   source venv/bin/activate"
echo ""
echo "2. 处理'相机视野影响鼠标速度'系列视频:"
echo "   python run_processing.py --task gaming"
echo ""
echo "3. 生成方言主播模板:"
echo "   python run_processing.py --task dialect"
echo ""
echo "有关更多信息，请参阅README.md" 